<?php

#4 512345 678906

function barcode_zero($n) {
	$o = "";
	for($i=0;$i<$n;$i++)$o.="0";
	return $o;
}

function barcode_fill($n,$s) {
	$n = "".$n;
	$d = strlen($n);
	if ($d>$s) {
		return substr($n,$d-$s);
	} else {
		return barcode_zero($s-$d).$n;
	}
}

function barcode_root($n) {
	$n = "".$n;
	$i = 0;
	for($j=0;$j<strlen($n);$j++) {
		if ($n[$j]=="0")$i++;
	}
	return "".$i;
}

function barcode_new($c,$l,$p) {
	$must = barcode_fill($l,1).barcode_fill($p,6)."909".barcode_fill($c,2);
	$code = $must.barcode_root($must);
	return $code;
}

function barcode_is_valid($code) {
	if (strlen($code)>2) {
		return true;
	} else {
		return false;
	}
}

function barcode_have_identity($code,$id) {
	$code = barcode_explode($code);
	if ($code['identity'] == $id) {
		return true;	
	} else {
		return false;
	}
}

function barcode_have_layer($code,$id) {
	$code = barcode_explode($code);
	if ($code['layer'] == $id) {
		return true;	
	} else {
		return false;
	}
}


function barcode_explode($code) {
	return array(
		"control" 	=> $code[12],
		"identity"	=> substr($code,7,3),
		"client"	=> substr($code,10,2),
		"layer"		=> $code[0],
		"load"		=> substr($code,1,6),
	);
}
